package com.seepln.manage;
/**
 * Created by
 *
 * @auth: liqy
 * on 2021/4/21 9:17.
 * desc:
 */

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.seepln.util.PathUtil;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;

/**
 * @ClassName: ConnComponents
 * @Description:
 * 管理连接器组件
 * 1.将连接器实例jar中/lib中的多版本多类型的连接器驱动或类库取出；
 * 2.将取出的jar包放入统一管理的war包解压后根路径下的lib目录中
 * @Author: liqy
 * @CreateDate: 2021/4/21 9:17
 * @UpdateUser: liqy
 * @UpdateDate: 2021/4/21  9:17
 * @UpdateRemark: update TODO
 * @Version: 1.0
 */
public class ConnComponents {


    public static void geneConnComponents(String path, String targetPath) throws IOException {
        targetPath = PathUtil.getPathBySourcePath(targetPath);
        if (!FileUtil.exist(targetPath)) {
            Files.createDirectory(Paths.get(targetPath));
        }
        JarFile jarFile = new JarFile(path);
        List<JarEntry> jarEntries = jarFile.stream().filter(current -> !current.isDirectory() && current.getName().contains(".jar"))
                .collect(Collectors.toList());
        String finalTargetPath = targetPath;
        jarEntries.stream().forEach(jarEntry -> {
            try (InputStream inputStream = jarFile.getInputStream(jarEntry);
                 FileOutputStream output = new FileOutputStream(finalTargetPath + "/" +
                         jarEntry.getName().substring(jarEntry.getName().lastIndexOf("/")))) {
                IoUtil.copy(inputStream, output);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
